package com.chen.solution.starter.demo.config;

import com.chen.solution.starter.demo.MyStarter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * <p>desc: 自动配置类</p>
 *
 * @author chentian
 * @date 2022/3/26
 */
@Configuration
@EnableConfigurationProperties(StarterProps.class)
// 在类路径 classpath 下有指定的类的情况下进行自动配置
@ConditionalOnClass(MyStarter.class)
// 属性 matchIfMissing=true 时进行自动配置
@ConditionalOnProperty(prefix="spring.chen.starter",value="enabled",matchIfMissing = true)
public class StarterServiceAutoConfiguration {

    @Resource
    private StarterProps starterProps;

    @Bean
    // 在容器中没有指定 Bean 的情况下自动配置 MyStarter 类
    @ConditionalOnMissingBean(MyStarter.class)
    public MyStarter myStarter(){
        MyStarter myStarter = new MyStarter(starterProps);
        return myStarter;
    }

}
